package util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Random;
import java.util.TreeSet;
import java.util.concurrent.CountDownLatch;

public class thread_generator implements Runnable{
	
	final String DATA_DIR="data/";
	private CountDownLatch clock;
	private static Random random;
	private File file;
	private int wordnum;
	private int num;
    public thread_generator(File file, int wordnum , int num, CountDownLatch clock) {
		random = new Random();
		this.file =file;
		this.wordnum = wordnum;
		this.num =num;
		this.clock =clock;
	}
    public String get_random_str(int n)
	{
		StringBuffer word = new StringBuffer();
		int tempnum=0;
		char letter;
		for(int i =0 ;i< n;i++)
		{
			tempnum = Math.abs(random.nextInt(52));
			if(tempnum<26)
			{
				letter = (char) (tempnum+'A');
			}
			else
				letter = (char) (tempnum-26 +'a');
			word.append(letter);
		}
		return word.toString();
	}
	@Override
	public void run() {
		BufferedWriter bw;
		try {
			bw = new BufferedWriter(new FileWriter(DATA_DIR+this.num+".data"));
			int num = random.nextInt(this.wordnum/2)+ this.wordnum/2;
			int lnum=0;
			int letternum= 10;
			TreeSet<String> sortset = new TreeSet<String>();
			for(int j= 0 ;j<num;j++)
			{
				lnum= random.nextInt(letternum/2)+letternum/2;
				sortset.add(get_random_str(lnum));
			}
			Iterator<String> iterator = sortset.iterator();
			while(iterator.hasNext())
			{
				bw.write(iterator.next());
				bw.newLine();
			}
				bw.close();
			clock.countDown();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}


